<template>
  <div>
    <header class="app-header">面试宝典</header>
    <div class="core-container">
      <div class="scroll-wrapper" ref="scroll">
        <div class="scroll-content">
          <div class="scroll-item" data-type="0">
            <div
              @touchstart.capture="doTouchStart"
              @touchend.capture="doTouchEnd"
              @click="go2next"
              class="list-box"
            >
              abc1
            </div>
            <div @click="doClick" class="del-btn">删除</div>
          </div>
          <div class="scroll-item" data-type="0">
            <div
              @touchstart.capture="doTouchStart"
              @touchend.capture="doTouchEnd"
              @click="go2next"
              class="list-box"
            >
              abc2
            </div>
            <div @click="doClick" class="del-btn">删除</div>
          </div>
          <div class="scroll-item" data-type="0">
            <div
              @touchstart="doTouchStart"
              @touchend="doTouchEnd"
              @click="go2next"
              class="list-box"
            >
              abc3
            </div>
            <div @click="doClick" class="del-btn">删除</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      endX: 0,
    };
  },
  methods: {
    doTouchStart(e) {
      this.startX = e.touches[0].clientX;
      // console.log(this.startX);
    },
    doTouchEnd(e) {
      let parentElement = e.currentTarget.parentElement;
      this.endX = e.changedTouches[0].clientX;
      // console.log(this.endX);
      // console.dir(parentElement);
      // 左滑
      if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
        this.resetSlide();
        parentElement.dataset.type = 1;
      }
      // 右滑
      if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
        this.resetSlide();
        parentElement.dataset.type = 0;
      }
      this.startX = 0;
      this.endX = 0;
    },
    //复位滑动状态
    resetSlide() {
      let listItems = document.querySelectorAll(".scroll-item");
      // 复位
      for (let i = 0; i < listItems.length; i++) {
        listItems[i].dataset.type = 0;
      }
    },

    doClick() {
      console.log("abc");
    },

    go2next() {
      if (this.checkSlide()) {
        this.resetSlide();
      } else {
        console.log("next");
      }
    },

    //判断当前是否有滑块处于滑动状态
    checkSlide() {
      let listItems = document.querySelectorAll(".scroll-item");
      for (let i = 0; i < listItems.length; i++) {
        if (listItems[i].dataset.type == 1) {
          return true;
        }
      }
      return false;
    },
  },
};
</script>

<style scoped>
.core-container .scroll-wrapper {
  height: calc(100vh - 121px);
  overflow: hidden;
}

.core-container .scroll-wrapper .scroll-item {
  height: 50px;
  line-height: 50px;
  font-size: 15px;
  border-bottom: 1px solid #eee;
  text-indent: 5px;
  /* white-space: nowrap; */
  /* text-overflow: ellipsis; */
  /* overflow: hidden; */
  position: relative;
  transition: all 0.2s;
}

.core-container .scroll-wrapper .scroll-item.study {
  color: #999;
}

.core-container .scroll-wrapper .scroll-item:nth-child(2n) {
  background-color: #f3f5f7;
}
.core-container .scroll-wrapper .scroll-item:nth-child(2n + 1) {
  background-color: #d4dadc;
}

.pulldown-wrapper {
  position: absolute;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  transform: translateY(-100%) translateZ(0);
  text-align: center;
  color: #999;
}

.pullup-tips {
  padding: 20px;
  text-align: center;
  color: #999;
}

/******************************************/

.list-box {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.del-btn {
  background-color: red;
  position: absolute;
  color: #fff;
  width: 60px;
  text-align: center;
  top: 0;
  right: -60px;
}

.scroll-item[data-type="0"] {
  transform: translate3d(0, 0, 0);
}
.scroll-item[data-type="1"] {
  transform: translate3d(-60px, 0, 0);
}
</style>